﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PIM_ATE.Electrical;

namespace PIM_ATE
{
    public partial class FrmConfigNewSelect : Form
    {
        public FrmConfigNewSelect(int iType)
        {
            InitializeComponent();
            m_iType = iType;
            if (m_iType == 1)
            {
                this.Text = "电性能配置可选";
                this.btnOK.Text = "电性能上传";
                btnSpecial.Visible = true;
            }
            else
            {
                this.Text = "互调配置可选";
                this.btnOK.Text = "互调上传";
                btnSpecial.Visible = false;
            }
        }

        int m_iType;
        string m_sProcode;
        string m_sMOID;
        DataTable m_dtConfig;
        ClsDataSpecial m_aSpecial;

        private void FrmConfigSelect_Load(object sender, EventArgs e)
        {
            m_sMOID = "";
            btnSetMOID.Enabled = true;
            textMOID.Enabled = true;
            btnOK.Enabled = false;
            btnSpecial.Enabled = false;

            //m_aSpecial = new ClsDataSpecial();
            //m_aSpecial.bEnable = false;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (m_sMOID == "")
            {
                MessageBox.Show("当前MOID不正确");
                return;
            }

            string selectInfo = "";
            for (int i = 0; i < dvgConfig.Rows.Count; i++)
            {
                if ((bool)dvgConfig.Rows[i].Cells[0].Value)
                {
                    selectInfo += "1";
                }
                else
                {
                    selectInfo += "0";
                }
            }

            ClsDataConfigSelectBuilder selectObj = new ClsDataConfigSelectBuilder(GDefine.DBNamePIM);
            selectObj.construct(m_sMOID);
            if (m_iType == 0)
            {
                if (selectObj.updatePIMSelect(m_sProcode, selectInfo))
                {
                    this.DialogResult = DialogResult.OK;
                }
            }
            else
            {
                if (selectObj.updateELECSelect(m_sProcode, selectInfo, m_aSpecial))
                {
                    this.DialogResult = DialogResult.OK;
                }
            }
        }

        private void checkAll_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < dvgConfig.Rows.Count; i++)
            {
                dvgConfig.Rows[i].Cells[0].Value = checkAll.Checked;
            }
        }

        private void btnSetMOID_Click(object sender, EventArgs e)
        {
            m_sProcode = "";
            ClsDataResultForAntennaBuilder resultObj = new ClsDataResultForAntennaBuilder(GDefine.DBNamePIM);
            m_sProcode = resultObj.getProcodeByMOID(textMOID.Text);
            if (m_sProcode == "" || m_sProcode == null)
            {
                MessageBox.Show("无法查询产品代码!");
                return;
            }

            m_sMOID = textMOID.Text;
            textMOID.Enabled = false;
            btnSetMOID.Enabled = false;
            btnOK.Enabled = true;
            btnSpecial.Enabled = true;

            if (m_iType == 0)
            {
                SetPimDataTable(m_sProcode);
            }
            else
            {
                var m_selectObj = new ClsDataConfigSelectBuilder(GDefine.DBNamePIM);
                m_selectObj.construct(textMOID.Text);
                m_aSpecial = m_selectObj.downladSpecial();
                SetElecDataTable(m_sProcode);
            }
        }

        private void SetPimDataTable(string sProCode)
        {      
            DataTable dtConfig;
            ClsDataConfigLocalBuilder cfgObj = new ClsDataConfigLocalBuilder(GDefine.DBNamePIM);
            cfgObj.constructByProcode(sProCode);
            if (cfgObj.construct(sProCode, "1.0"))
            {//根据选择下载配置
                if (cfgObj.download(out dtConfig) == null)
                {
                    MessageBox.Show("配置错误：" + sProCode);
                    return;
                }
            }
            else
            {
                MessageBox.Show("未发现配置：" + sProCode);
                return;
            }

            dvgConfig.ReadOnly = false;
            dvgConfig.RowHeadersVisible = false;
            dvgConfig.AllowUserToAddRows = false;
            dvgConfig.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dvgConfig.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dvgConfig.MultiSelect = false;

            List<string> names = new List<string>();
            names.Add("测试项目");
            names.Add("测试频段");
            names.Add("RCU序列");
            names.Add("角度");
            names.Add("测试要求");

            m_dtConfig = dtConfig.DefaultView.ToTable(false, names.ToArray());
            dvgConfig.DataSource = m_dtConfig;

            var m_selectObj = new ClsDataConfigSelectBuilder(GDefine.DBNamePIM);
            m_selectObj.construct(textMOID.Text);
            List<bool> enablePIM = m_selectObj.downloadPIMSelect();
            if (enablePIM == null)
            {
                MessageBox.Show("PIMSelect下载错误");
                return;
            }

            if (enablePIM.Count != 0 && dvgConfig.Rows.Count != enablePIM.Count)
            {
                MessageBox.Show("PIMSelect数据错误，请重新配置");
                enablePIM = new List<bool>();
            }

            initTable(enablePIM);
        }

        private void SetElecDataTable(string sProCode)
        {
            List<clsTestItem> lstConfig;
            DataTable dtConfig;
            var cfgObj = new Electrical.ClsDataSParamBuilder(GDefine.DBNamePIM);
            cfgObj.constructByProcode(sProCode);
            if (cfgObj.construct(sProCode, "1.0"))
            {//下载配置
                dtConfig = cfgObj.download(out lstConfig);
            }
            else
            {
                MessageBox.Show("未发现配置：" + sProCode);
                return;
            }

            dvgConfig.ReadOnly = false;
            dvgConfig.RowHeadersVisible = false;
            dvgConfig.AllowUserToAddRows = false;
            dvgConfig.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //dvgConfig.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            dvgConfig.MultiSelect = false;

            string[] m_arrShowList = new string[9];
            int n = 0;
            m_arrShowList[n++] = clsTestItem.chn_ParamName;
            m_arrShowList[n++] = clsTestItem.chn_Freq;
            //m_arrShowList[i++] = clsTestItem.chn_Band;
            m_arrShowList[n++] = clsTestItem.chn_PortA_Angle;
            m_arrShowList[n++] = clsTestItem.chn_PortB_Angle;
            m_arrShowList[n++] = clsTestItem.chn_PortA_RCU;
            m_arrShowList[n++] = clsTestItem.chn_PortB_RCU;
            m_arrShowList[n++] = clsTestItem.chn_PortA;
            m_arrShowList[n++] = clsTestItem.chn_PortB;
            m_arrShowList[n++] = clsTestItem.chn_FactSpec; 

            m_dtConfig = dtConfig.DefaultView.ToTable(false, m_arrShowList);
            dvgConfig.DataSource = m_dtConfig;

            var m_selectObj = new ClsDataConfigSelectBuilder(GDefine.DBNamePIM);
            m_selectObj.construct(textMOID.Text);
            List<bool> enablePIM = m_selectObj.downloadELECSelect();
            if (enablePIM == null)
            {
                MessageBox.Show("ELECSelect下载错误");
                return;
            }

            if (enablePIM.Count != 0 && dvgConfig.Rows.Count != enablePIM.Count)
            {
                MessageBox.Show("ELECSelect数据错误，请重新配置");
                enablePIM = new List<bool>();
            }

            initTable(enablePIM);
        }

        private void initTable(List<bool> enableList)
        {
            if (dvgConfig.Columns[0].HeaderText != "")
            {
                var colCheck = new DataGridViewCheckBoxColumn();
                dvgConfig.Columns.Insert(0, colCheck);             
            }

            for (int i = 0; i < dvgConfig.Rows.Count; i++)
            {
                if (enableList.Count == 0)
                    dvgConfig.Rows[i].Cells[0].Value = true;
                else
                    dvgConfig.Rows[i].Cells[0].Value = enableList[i];
            }

            for (int i = 0; i < dvgConfig.Columns.Count; i++)
            {
                dvgConfig.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
                if (i == 0)
                    dvgConfig.Columns[0].ReadOnly = false;
                else
                    dvgConfig.Columns[i].ReadOnly = true;
            }
            checkAll.Checked = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnReSetMOID_Click(object sender, EventArgs e)
        {
            m_sMOID = "";
            btnSetMOID.Enabled = true;
            textMOID.Enabled = true;
            btnOK.Enabled = false;
            btnSpecial.Enabled = false;

            if (m_dtConfig != null)
                m_dtConfig.Clear();
        }

        private void btnSpecial_Click(object sender, EventArgs e)
        {
            FrmDataSpecial frmSpecial = new FrmDataSpecial();
            frmSpecial.checkEnable.Checked = m_aSpecial.bEnable;
            frmSpecial.radioPIMTrue.Checked = m_aSpecial.bNeedCheckPIM;
            frmSpecial.radioPIMFalse.Checked = !m_aSpecial.bNeedCheckPIM;
            frmSpecial.nudMax.Value = Convert.ToDecimal(m_aSpecial.fMaxVaule);
            frmSpecial.nudMin.Value = Convert.ToDecimal(m_aSpecial.fMinVaule);
            if (frmSpecial.ShowDialog() == DialogResult.OK)
            {
                m_aSpecial.SetDataSpecial(frmSpecial.checkEnable.Checked,
                                        frmSpecial.radioPIMTrue.Checked,
                                        Convert.ToSingle(frmSpecial.nudMax.Value),
                                        Convert.ToSingle(frmSpecial.nudMin.Value));
            }
        }
    }
}
